import {
	contextBridge,
	ipcRenderer
} from 'electron'
import {
	electronAPI
} from '@electron-toolkit/preload'

// 渲染器的定制api
const api = {
	node: () => process.versions.node,
	chrome: () => process.versions.chrome,
	electron: () => process.versions.electron,
	BrowserWindow: (type, data) => ipcRenderer.send('set-BrowserWindow', type, data),
	windowCreate: (args) => ipcRenderer.send('window-new', args)
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.

if (process.contextIsolated) {
	try {
		contextBridge.exposeInMainWorld('electron', electronAPI)
		contextBridge.exposeInMainWorld('api', api)
	} catch (error) {
		console.error(error)
	}
} else {
	window.electron = electronAPI
	window.api = api
}